const app = getApp();
var h = getApp().api;
Page({
  data: {
    uploaderList: [],
    uploaderNum: 0,
    showUpload: true,
    text:"涉及色情暴力",
    type:0,
    desc_lenthg:0,
    submit_data:{
      type:1,
      posts_id:0,
      decs:'',
      mobile:'',
      pic_url:[]
    },
  },
  onLoad: function (t) {
   
    getApp().page.onLoad(this, t);

    if (this.data.store.is_shenhe == 0) {
      wx.setNavigationBarTitle({
        title: '投诉'
      })
    }
    if (t.posts_id > 0){
      var submit_data = this.data.submit_data;
      submit_data.posts_id = t.posts_id;
      this.setData({
        submit_data: submit_data
      })
    }else{
      wx.navigateBack({
        delta: 1
      })
    }
  },
  openaction(){
   var itemList=[
      '涉及色情暴力',
      '涉及欺诈造价', 
      '涉及违禁信息',
      '感觉被冒犯或歧视',
      '涉及侵权',
      '其他']//显示的列表项
    var that=this;
    wx.showActionSheet({
      itemList: itemList,
      success: function (res) {
        if (!res.cancel) {
          var submit_data = that.data.submit_data;
          submit_data.type = res.tapIndex + 1;
          that.setData({
            text: itemList[res.tapIndex],
            submit_data: submit_data
          })
         
        }
      },
      fail: function (res) { },
      complete: function (res) { }
    })
     },
  // 删除图片
  clearImg: function (e) {
    var nowList = [];//新数据
    var uploaderList = this.data.uploaderList;//原数据

    for (let i = 0; i < uploaderList.length; i++) {
      if (i == e.currentTarget.dataset.index) {
        continue;
      } else {
        nowList.push(uploaderList[i])
      }
    }
    this.setData({
      uploaderNum: this.data.uploaderNum - 1,
      uploaderList: nowList,
      showUpload: true
    })
  },
  //展示图片
  showImg: function (e) {
    var that = this;
    wx.previewImage({
      urls: that.data.uploaderList,
      current: that.data.uploaderList[e.currentTarget.dataset.index]
    })
  },
  //上传图片
  upload: function (e) {
    var that = this;
    wx.chooseImage({
      count: 4 - that.data.uploaderNum, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        let uploaderList = that.data.uploaderList.concat(tempFilePaths);
        if (uploaderList.length == 4) {
          that.setData({
            showUpload: false
          })
        }
        that.setData({
          uploaderList: uploaderList,
          uploaderNum: uploaderList.length,
        })
      }
    })
  },

  submit_report:function (t) {
    var that = this;
    function i(e) {
      if (e == n.length) return o();
      var ii = 0;
      for (var s in n) !function (oo) {
        wx.uploadFile({
          url: h.default.upload_image,
          name: "image",
          formData: r,
          filePath: n[oo],
          complete: function (t) {
            if (t.data) {
              var s = JSON.parse(t.data);
              if (0 == s.code && (submit_data.pic_url[oo] = s.data.url)) {
                that.setData({
                  submit_data: submit_data
                })
                ii++;
              }
            }
            if (ii == n.length) return o();
          }
        });
      }(s);
    }
    function o() {
      getApp().request({
        url: h.posts.posts_report,
        method: "post",
        data: that.data.submit_data,
        success: function (t) {
          wx.hideLoading()
          if(t.code==0){
            wx.showModal({
              title: "提示",
              content: t.info,
              showCancel: !1,
              success: function (e) {
                e.confirm && wx.navigateBack({
                  delta: 1
                })
              }
            })
          }else{
            wx.showToast({
              title: t.info,
              icon: 'none',
              duration: 1500
            })
          }
           
        }
      });
    }

    var n = this.data.uploaderList,r={},submit_data = this.data.submit_data;
    // 判断手机号是否符合标准
    if (submit_data.mobile != ''){
      if (submit_data.mobile.length != 11) {
        wx.showToast({
          title: '手机号长度有误！',
          icon: 'none',
          duration: 1500
        })
        return false;
      }

      var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
      if (!myreg.test(submit_data.mobile)) {
        wx.showToast({
          title: '手机号有误！',
          icon: 'none',
          duration: 1500
        })
        return false;
      }
    }
    if (submit_data.decs == ''){
      wx.showModal({
        title: "提示",
        content: "请填写投诉内容",
        showCancel: !1,
        success: function (e) {}
      })
    }else{
      wx.showLoading({
        title: '提交中',
      })
      if (n.length == 0){
        o()
      }else{
        i(0)
      }
    }
  },
  bindContentInput: function (t) {
    if (this.data.submit_data.decs.length > 100){
      wx.showToast({
        icon: 'none',
        title: '投诉内容已达到长度限制'
      })
      return false;
    }else{
      this.data.submit_data.decs = t.detail.value, this.setData({
        submit_data: this.data.submit_data,
        desc_lenthg: this.data.submit_data.decs.length
      });
    }
  },
  bindMobileInput: function (t) {
    this.data.submit_data.mobile = t.detail.value, this.setData({
      submit_data: this.data.submit_data,
    });
  },
})